package com.ruoyi.attendance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.attendance.dto.AttendanceRecordQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.attendance.domain.AttendanceRecord;
import com.ruoyi.attendance.service.IAttendanceRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工考勤记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-05
 */
@Slf4j
@RestController
@RequestMapping("/attendance/record")
public class AttendanceRecordController extends BaseController
{
    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    /**
     * 查询员工考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceRecord attendanceRecord)
    {
        startPage();
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordList(attendanceRecord);
        return getDataTable(list);
    }

    /**
     * 导出员工考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:export')")
    @Log(title = "员工考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceRecord attendanceRecord)
    {
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordList(attendanceRecord);
        ExcelUtil<AttendanceRecord> util = new ExcelUtil<AttendanceRecord>(AttendanceRecord.class);
        util.exportExcel(response, list, "员工考勤记录数据");
    }

    /**
     * 获取员工考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(attendanceRecordService.selectAttendanceRecordById(id));
    }

    /**
     * 查询某个员工的一月的考勤汇总
     */

    @GetMapping("/summary")
    public AjaxResult getSummary(@RequestBody AttendanceRecordQueryDTO recordQueryDTO) {

        log.info("这是/attendance/record/summary接口的get请求\n参数recordQueryDTO={}", recordQueryDTO);
        return success(attendanceRecordService.getSummary(recordQueryDTO));

    }


    /**
     * 新增员工考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:add')")
    @Log(title = "员工考勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceRecord attendanceRecord)
    {
        log.info("这是/attendance/record接口的post请求\n参数attendanceRecord={}", attendanceRecord);
        return toAjax(attendanceRecordService.insertAttendanceRecord(attendanceRecord));
    }

    /**
     * 修改员工考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:edit')")
    @Log(title = "员工考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceRecord attendanceRecord)
    {
        return toAjax(attendanceRecordService.updateAttendanceRecord(attendanceRecord));
    }

    /**
     * 删除员工考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:record:remove')")
    @Log(title = "员工考勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(attendanceRecordService.deleteAttendanceRecordByIds(ids));
    }
}
